var tempbm;
var tempstore;
function initPage() {
    $.ajax({
        url: "./goSHGL/initPage", //请求的url地址
        xhrFields: {
            withCredentials: true
        },
        async:false,
        dataType: "json", //返回格式为json
        type: "GET", //请求方式
        processData: false, //对表单data数据是否进行序列化
        contentType: false, //dataType设置你收到服务器数据的格式
        headers: {
            'Content-Type': 'application/json'
        },
        beforeSend: function() {
            //请求前的处理
        },
        success: function(data) {
            if(data.status==0){
                // top.location.href = "../../login.html";
                window.location.href="./login.html";
            }else{
                var json = JSON.parse(data.data)
                var tempstorejson = json.data;
                tempbm = data.bm;
                tempstore = data.store;
                // if(data.store=='总部'){
                //     for(var i=0;i<tempstorejson.length;i++){
                //         var temp = {
                //             value: tempstorejson[i].Post,
                //             label: tempstorejson[i].Post
                //         };
                //         storejson.push(temp)
                //     }
                // }else{
                //     var temp = {
                //         value: data.store,
                //         label: data.store
                //     };
                //     storejson.push(temp)
                // }
            }
        },
        complete: function() {
            //请求完成的处理
        },
        error: function() {}
    });
}
Vue.component('v-distpicker', VDistpicker);

var vm = new Vue({
    el:"#app",
    data(){
        return {
            formItem: {
                province:'',
                city:'',
                area:'',
                Store: '',
                Bm: '',
                Name:'',
                Sex:'',
                Tel1:'',
                Tel2:'',
                Fixtel:'',
                Jiguan:'',
                CardNo:'',
                Birthday:'',
                YinOrYang:'',
                ZsProgress:'',
                PastDaiBrand:'',
                StartTime:'',
                Relations:'',
                ShStar:''
            },
            input2:'',
            model1: '',
            model1:false,
            options3: {
                disabledDate (date) {
                    return date;
                }
            },
        }
    },
    created: function () {
        initPage();
        this.formItem.Bm = tempbm;
        this.formItem.Store = tempstore;
    },
    methods:{
        changeProvince(data) {
            this.formItem.province = data.value;
        },
        changeCity(data) {
            this.formItem.city = data.value;
        },
        changeArea(data) {
            this.formItem.area = data.value;
        },
        //手机号验证
        isPoneAvailable(str) {
            var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
            if (!myreg.test(str)) {
                return false;
            } else {
                return true;
            }
        },
        //固定电话验证
        isFixPoneAvailable(str) {
            var myreg=/^0\d{2,3}-\d{7,8}(-\d{1,6})?$/;
            if (!myreg.test(str)) {
                return false;
            } else {
                return true;
            }
        },
        //身份证号验证
        isCardNoAvailable(str) {
            var myreg=/^[1-9]{1}[0-9]{14}$|^[1-9]{1}[0-9]{16}([0-9]|[xX])$/;
            if (!myreg.test(str)) {
                return false;
            } else {
                return true;
            }
        },
        saveSHInfo() {
            var self = this;
            var jsondata = self.formItem;
            var EditNameStr = parent.$("#uname1").text();
            var EditIDStr = parent.$("#userno1").text();
            jsondata.EditName = EditNameStr;
            jsondata.EditID = EditIDStr;

            //校验开始
            if(jsondata.Store==''||jsondata.Store==null){
                this.$Notice.warning({
                    title: '警告',
                    desc: '请选择中心店面'
                });
                return false;
            }
            if(jsondata.Bm==''||jsondata.Bm==null){
                this.$Notice.warning({
                    title: '警告',
                    desc: '请填写部门'
                });
                return false;
            }
            if(jsondata.Name==''||jsondata.Name==null){
                this.$Notice.warning({
                    title: '警告',
                    desc: '请填写姓名'
                });
                return false;
            }
            if(jsondata.Sex==''||jsondata.Sex==null){
                this.$Notice.warning({
                    title: '警告',
                    desc: '请选择性别'
                });
                return false;
            }
            if(jsondata.Tel1==''||jsondata.Tel1==null){
                this.$Notice.warning({
                    title: '警告',
                    desc: '请输入手机号'
                });
                return false;
            }
            if(!this.isPoneAvailable(jsondata.Tel1)){
                this.$Notice.warning({
                    title: '警告',
                    desc: '请输入正确格式的手机号'
                });
                return false;
            }

            if(jsondata.Tel2!=''&&jsondata.Tel2!=null){
                if(!this.isPoneAvailable(jsondata.Tel2)){
                    this.$Notice.warning({
                        title: '警告',
                        desc: '请输入正确格式的手机号2'
                    });
                    return false;
                }
            }
            /*if(jsondata.Fixtel!=''&&jsondata.Fixtel!=null){
                this.$Notice.warning({
                    title: '警告',
                    desc: '请输入同行业情况'
                });
            }*/
            if(jsondata.CardNo==''||jsondata.CardNo==null){
                this.$Notice.warning({
                    title: '警告',
                    desc: '请输入身份证号'
                });
                return false;
            }

            if(!this.isCardNoAvailable(jsondata.CardNo)){
                this.$Notice.warning({
                    title: '警告',
                    desc: '请输入正确格式的身份证号'
                });
                return false;
            }

            if(jsondata.Birthday==null||jsondata.Birthday==''){
                this.$Notice.warning({
                    title: '警告',
                    desc: '请选择生日'
                });
                return false;
            }

            if(jsondata.YinOrYang==null||jsondata.YinOrYang==''){
                this.$Notice.warning({
                    title: '警告',
                    desc: '请选择阴/阳历'
                });
                return false;
            }

            /*if(jsondata.StartTime==null||jsondata.StartTime==''){
                this.$Notice.warning({
                    title: '警告',
                    desc: '请输入开始经营家具时间'
                });
                return false;
            }*/
            if(jsondata.StartTime!=null&&jsondata.StartTime!=""){
                var tempStartTimee = new Date(jsondata.StartTime);
                jsondata.StartTime = tempStartTimee.toLocaleDateString();
            }else{
                jsondata.StartTime = null;
            }

            /*if(jsondata.Relations==null||jsondata.Relations==''){
                this.$Notice.warning({
                    title: '警告',
                    desc: '请输入所获荣誉'
                });
                return false;
            }
            if(jsondata.ShStar==null||jsondata.ShStar==''){
                this.$Notice.warning({
                    title: '警告',
                    desc: '请选择商户星级'
                });
                return false;
            }*/
            //校验结束

            var data_json = JSON.stringify(jsondata)
            $.ajax({
                url: "./goSHGL/saveSHInfo", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.status=='1'){
                        toastr.success('保存成功');
                        // self.formItem.Store = '';
                        // self.formItem.Bm = '';
                        self.formItem.province = '';
                        self.formItem.city = '';
                        self.formItem.area = '';
                        self.formItem.Name = '';
                        self.formItem.Sex = '';
                        self.formItem.Tel1 = '';
                        self.formItem.Tel2 = '';
                        self.formItem.Fixtel = '';
                        self.formItem.Jiguan = '';
                        self.formItem.CardNo = '';
                        self.formItem.Birthday = '';
                        self.formItem.YinOrYang = '';
                        self.formItem.ZsProgress = '';
                        self.formItem.PastDaiBrand = '';
                        self.formItem.StartTime = '';
                        self.formItem.Relations = '';
                        self.formItem.ShStar = '';

                    }else if(data.status=='-1'){
                        toastr.warning('保存失败，已有当前商户');
                    }else{
                        toastr.error('保存失败，请重新录入');
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });

        },
        //取消
        goBack(){
            window.location.href = "./sh_search.html";
        }
    }
})